package lab_16.com.acme.bankapp.service.bank;

import java.io.IOException;
import java.io.InputStreamReader;

import lab_16.com.acme.bankapp.domain.bank.Bank;

public class Dialogue {

	private Bank bank;

	public Dialogue(Bank bank) {
		this.bank = bank;
	}

	public void run() {
		String command;
		help();
		
		while(true) {
			System.out.println();
			command = readCommand();
			
			switch (command) {
			case "getBankCredit":
				System.out.printf("Overall bank credit is: %.2f\n",BankReport.getBankCredit(bank));
				break;
			case "getNumberOfAccounts":
				System.out.printf("Bank has %d accounts.\n",BankReport.getNbOfAccounts(bank));
				break;
			case "getNumberOfCustomers":
				System.out.printf("Bank has %d customers overall.\n",BankReport.getNbOfCustomers(bank));
				break;
			case "exit":
				System.out.println("Exiting.");
				System.exit(0);
			default:
				System.out.printf("Command %s is not supported.\n",command);
				help();
			}
		}
		
	}
	
	private String readCommand() {
		StringBuilder sb = new StringBuilder();
		InputStreamReader input = new InputStreamReader(System.in);
		Character symbol = 0;
		while(true) {
			try {
				symbol = (char) input.read();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (symbol.equals('\n')) break;
			sb.append(symbol);
		}
		return sb.toString().trim();
	}

	private void help() {
		System.out.println("-----\nStatistics mode.\n" +
				"Enter one of the following commands to receive statistics.\n" +
				"getBankCredit\n" +
				"getNumberOfAccounts\n" +
				"getNumberOfCustomers\n" +
				"exit\n\n");
	}

}
